package com.example.demo;

import java.io.IOException;
import java.net.Socket;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends ListActivity {
	public static String[] light = new String[] { "Light 1", "Light 2",
			"Light 3", "Light 4", "Light 5", "Light 6", "Light 7", "Light 8", };
	public final static int icon[] = { R.drawable.icon1, R.drawable.icon2,
			R.drawable.icon3, R.drawable.icon4, R.drawable.icon5,
			R.drawable.icon6, R.drawable.icon7, R.drawable.icon8 };
	private Protocol protocol = new Protocol();
	private Socket sk;
	private Myapplication app;
	private String status = "";
	private String timeon = "";
	private String timeoff = "";
	private String auto = "";
	private String progress = "";
	private String settimeon = "";
	private String settimeoff = "";
	private ProgressDialog loading;
	private Integer posi;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (Myapplication) getApplication();
		sk = app.getSocket();
		Loadcof();
		IconAdapter adapter = new IconAdapter(this, R.id.tvtext, light);
		this.setListAdapter(adapter);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		posi =position;
		loading = ProgressDialog.show(ListViewActivity.this,"","Loading..",true);
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				try {
					protocol.sendMessages(app.socket, String.valueOf(posi),
							Protocol.LIGHTDEVICE);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					String data = protocol.Receive(sk);
					if (protocol.findTypeMsg(data) == Protocol.STATUSDATABASE) {
						database(data);
					}
					
					Bundle sendBundle = new Bundle();
					sendBundle.putString("status",status);
					sendBundle.putString("settimeon",settimeon);
					sendBundle.putString("settimeoff",settimeoff);
					sendBundle.putString("timeon",timeon);
					sendBundle.putString("timeoff",timeoff);
					sendBundle.putString("auto",auto);
					sendBundle.putString("progress",progress);
					sendBundle.putInt("position",posi);
					Intent intent = new Intent(ListViewActivity.this, LightDivece.class);
					intent.putExtras(sendBundle);
					loading.dismiss();
					startActivity(intent);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
		

	}

	public void Loadcof() {
		SharedPreferences editPreference = this.getSharedPreferences(
				"DATAUSER", Activity.MODE_PRIVATE);
		light[0] = editPreference.getString("LIGHT1", "light1");
		light[1] = editPreference.getString("LIGHT2", "light2");
		light[2] = editPreference.getString("LIGHT3", "light3");
		light[3] = editPreference.getString("LIGHT4", "light4");

	}

	public void database(String st) {
		// <STATUSDATABASE>1_120:00_023:00_0_040</STATUSDATABASE>
		status     = st.substring(16, 17);
		settimeon  = st.substring(18, 19);
		timeon     = st.substring(19, 24);
		settimeoff = st.substring(25, 26);
		timeoff    = st.substring(26, 31);
		auto       = st.substring(32, 32);
		progress   = st.substring(34, 37);
		Log.i("status",status);
		Log.i("settimeon",settimeon);
		Log.i("timeon",timeon);
		Log.i("settimeoff",settimeoff);
		Log.i("timeoff",timeoff);
		Log.i("auto",auto);
		Log.i("progress",progress);
	}
}
